<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Parent Model for performing CRUD operations.
 * @package core
 * @category Model
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2014
 */
class Test extends CI_Model {

    /**
     * @var string
     * Name of the database table to be manipulated. 
     */
    var $table = '';
    
    /**
     * @var string
     * Primary key of the table. 
     */
    var $primary_key = 'id';
    
    
    var $result = '';
    
    /**
     * Constructor Method.
     */
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    /**
     * Retrieves one record from a database table using its id.
     * @access public
     * @param int $id The id of the content being retrieved from the database.
     * @param string $alias Comma Separated database attributes same to datatables header to be retrieved.
     * @return array|null Returns the row from the database as an array if id is found, otherwise null.
     */
    function find($id, $alias = '*')
    {
        $this->db->select($alias, FALSE);
        $this->db->where($this->primary_key, $id);
        $query = $this->db->get($this->table);

        $result = $query->result_array();
        $this->result = (count($result) > 0 ? $result[0] : array());
        return $this;
    }
    
    function has_many($table, $local_key = 'id', $parent_key = 'some_id')
    {
        if( isset($this->result[0]) && is_array($this->result[0]) )
        {
            $ids = array_column( $this->result, $parent_key );
        } else
        {
            $ids = array( $this->result[ $parent_key ] );
        }
        
        $this->db->where_in($local_key, $ids);
        $query = $this->db->get($table);
        
        $this->result = $query->result_array();
        return $this;
    }
    
    function get()
    {
        return $this->result;
    }

}


//class Crud_Result {
//    
//    protected $result = array();
//    
//    function set_result($result)
//    {
//        $this->result = $result;
//    }
//    
//    function get_result()
//    {
//        return $this->result;
//    }
//}

/* End of file Crud.php */
/* Location: ./system/application/core/Crud.php */